﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;

namespace MstscHelperApp
{
    public partial class Form3 : Form
    {
        string mstscjsonPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "mstscdata.json");
        public string IP { get; set; } // 用于存储要修改的IP地址
        public Form3()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            List<MsctcInfo> mstscInfos = new List<MsctcInfo>();
            #region 验证输入
            if (string.IsNullOrEmpty(textBox1.Text))
            {
                MessageBox.Show("请输入分组");
                return;
            }
            if (string.IsNullOrEmpty(textBox2.Text))
            {
                MessageBox.Show("请输入远程主机地址");
                return;
            }
            if (string.IsNullOrEmpty(textBox3.Text))
            {
                MessageBox.Show("请输入登录账号");
                return;
            }
            if (string.IsNullOrEmpty(textBox4.Text))
            {
                MessageBox.Show("请输入登录密码");
                return;
            }
            #endregion
            #region 获取本地数据
            if (File.Exists(mstscjsonPath))
            {
                //如果存在
                string jsonData = File.ReadAllText(mstscjsonPath);
                if (!string.IsNullOrEmpty(jsonData))
                {
                    mstscInfos = JsonHelper.JsonToT<List<MsctcInfo>>(jsonData);
                    save(mstscInfos);
                    MessageBox.Show("修改成功");
                    this.DialogResult = DialogResult.OK;
                }
            }
            else
            {
                MessageBox.Show("数据文件不存在，请先添加数据。");
                this.DialogResult = DialogResult.OK;
            }
            #endregion 
            this.Close();

        }

        public void save(List<MsctcInfo> mstscInfos)
        {
            #region 修改数据
            if (mstscInfos.Exists(m => m.IP == textBox2.Text))
            {
                var info = mstscInfos.FirstOrDefault(m => m.IP == textBox2.Text);
                info.Group = textBox1.Text;
                info.IP = textBox2.Text;
                info.Desc = textBox5.Text;
                info.UserName = textBox3.Text;
                info.Password = textBox4.Text;
            }
            string json = JsonHelper.TtoJson(mstscInfos);
            if (File.Exists(mstscjsonPath))
            {
                File.Delete(mstscjsonPath);
            }
            File.WriteAllText(mstscjsonPath, json, Encoding.UTF8);
            #endregion
        }

        private void Form3_Load(object sender, EventArgs e)
        {
            // 窗体加载时，设置文本框的初始值
            if (File.Exists(mstscjsonPath))
            {
                string jsonData = File.ReadAllText(mstscjsonPath);
                if (!string.IsNullOrEmpty(jsonData))
                {
                    var mstscInfos = JsonHelper.JsonToT<List<MsctcInfo>>(jsonData);
                    var info = mstscInfos.FirstOrDefault(m => m.IP == IP);
                    textBox1.Text = info.Group; // 设置默认分组
                    textBox2.Text = IP; // 设置要修改的IP地址
                    textBox2.Enabled = false; // 禁止修改IP地址
                    textBox3.Text = info.UserName; // 设置默认用户名
                    textBox4.Text = info.Password; // 设置默认密码
                    textBox5.Text = info.Desc; // 设置默认描述
                }
            }


        }
    }
}
